## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-

srcdir = '.'
blddir = 'build'

from heapq import merge

def headers():
    private_headers = ['lib/csum.h','lib/poll-loop.h', 'lib/rconn.h', 'lib/queue.h',
                       'lib/stp.h', 'lib/compiler.h', 'lib/util.h', 'lib/vconn.h',
                       'lib/xtoxll.h', 'switch/chain.h', 'switch/table.h', 'switch/datapath.h',
                       'lib/ofpbuf.h', 'switch/switch-port.h', 'lib/red-black-tree.h',
                       'lib/misc.h', 'lib/stack.h', 'switch/dp_act.h', 'switch/switch-flow.h', 
                       'lib/flow.h', 'lib/list.h', 'switch/pt_act.h',
                       'lib/packets.h', 'lib/random.h', 'lib/timeval.h', 'lib/type-props.h']
    return private_headers


def set_options(opt):
    opt.tool_options('compiler_cc')

def configure(conf):
    conf.check_tool('compiler_cc')

    import os
    import shutil
    try:
        shutil.rmtree('include/openflow/private')
    except:
        pass
    try:
        os.makedirs('include/openflow/private/')
    except:
        pass
    for header in headers():
        shutil.copy(header, 'include/openflow/private/' + os.path.basename (header))

def build(bld):
    # Build the OpenFlow C static library from the software implementation distribution
    of = bld.new_task_gen('cc', 'cstaticlib')
    of.source = [
        'lib/command-line.c',
        'lib/csum.c',
        'lib/daemon.c',
        'lib/dhcp.c',
        'lib/dhcp-client.c',
        'lib/dirs.c',
        'lib/dpif.c',
        'lib/dynamic-string.c',
        'lib/fatal-signal.c',
        'lib/fault.c',
        'lib/flow.c',
        'lib/freelist.c',
        'lib/hash.c',
        'lib/hmap.c',
        'lib/learning-switch.c',
        'lib/list.c',
        'lib/mac-learning.c',
        'lib/misc.c',
        'lib/mpls-fib.c',
        'lib/mpls-switch.c',
        'lib/netdev.c',
        'lib/netlink.c',
        'lib/ofpbuf.c',
        'lib/ofp-print.c',
        'lib/poll-loop.c',
        'lib/port-array.c',
        'lib/queue.c',
        'lib/random.c',
        'lib/rconn.c',
        'lib/read-mpls-fib.c',
        'lib/red-black-tree.c',
        'lib/signals.c',
        'lib/socket-util.c',
        'lib/stack.c',
        'lib/stp.c',
        'lib/timeval.c',
        'lib/util.c',
        'lib/vconn.c',
        'lib/vconn-mpls.c',
        'lib/vconn-netlink.c',
        'lib/vconn-stream.c',
        'lib/vconn-tcp.c',
        'lib/vconn-unix.c',
        'lib/vlog.c',
        'lib/vlog-socket.c',
        'switch/chain.c',
        'switch/crc32.c',
        'switch/datapath.c',
        'switch/dp_act.c',
        'switch/er_act.c',
        'switch/nx_act.c',
        'switch/pt_act.c',
        'switch/switch.c',
        'switch/switch-flow.c',
        'switch/switch-port.c',
        'switch/table-hash.c',
        'switch/table-linear.c',
    ]
    of.includes = [
	'.',
	'..',
	'include/.',
	'include/openflow/.',
	'lib/.',
	'switch/.',
	'/usr/include/libxml2',
    ]
    of.env.append_value('CCFLAGS', '-fPIC')
    of.env.append_value('CCFLAGS', '-DNS3')
    of.target = 'openflow'

    # finally, let's install 
    bld.install_files('${PREFIX}/lib', 'libopenflow.a')
    start_dir = bld.path.find_dir('include/openflow')
    bld.install_files('${PREFIX}/include/openflow', start_dir.ant_glob('**/*.h'), cwd=start_dir)
    bld.install_files('${PREFIX}/include/openflow/private', headers())
